package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQueryObject;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service
@Transactional
class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementMapper statementMapper;




    @Override
    public TablePageInfo<StatementItem> query(StatementItemQueryObject qo,Long statementId) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<StatementItem> statementItems = statementItemMapper.selectForList(qo,statementId);

        return new TablePageInfo<StatementItem>(statementItems);
    }
    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void saveItems(List<StatementItem> statementItems) {
        //保存用户消费了那些服务
        //保存之前删除对应用户之前的消费数据,再执行保存
        BigDecimal totalAmount = new BigDecimal("0.00"); //总消费金额
        BigDecimal totalQuantity = new BigDecimal("0");  //服务项数量

        //把最后一行的非item数据删除并取出
        StatementItem remove = statementItems.remove(statementItems.size() - 1);


        statementItemMapper.deleteByStartment(remove.getStatementId());
        //保存产品用户关系表,还要去添加结算表的属性
        if (statementItems!=null &&statementItems.size()>0){
            for (StatementItem statementItem : statementItems) {
                statementItemMapper.insert(statementItem);
                //每次记录数量
                totalQuantity = totalQuantity.add(new BigDecimal(statementItem.getItemQuantity()));
                //每次循环产品价格乘于数量
                totalAmount = totalAmount.add(statementItem.getItemPrice().multiply(new BigDecimal(statementItem.getItemQuantity())));
            }
        }

        //折扣价
        BigDecimal discountAmount = remove.getItemPrice();
        //判断折扣价不能违法//compareTo比较器  总金额不能比折扣小
        if (totalAmount.compareTo(discountAmount)<0){
            throw new BusinessException("优惠价格异常");
        }
        //保存操作
        Statement statement = statementMapper.selectByPrimaryKey(remove.getStatementId());
        //折扣
        statement.setDiscountAmount(discountAmount);
        //总金额
        statement.setTotalAmount(totalAmount);
        //数量 服务数量
        statement.setTotalQuantity(totalQuantity);
        //修改操作
        statementMapper.updateByStatement(statement);

    }




    @Override//保存
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }




    @Override//删除
    public void deleteBatch(String ids) {

        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            //硬删除
            statementItemMapper.deleteByPrimaryKey(dictId);

        }
    }
}
